@extends('common.base-master')

@section('html-head')
@endsection

@section('html-body')
    <div class="reset-page">
        <form action="post" class="">
			<h5>通过手机验证找回密码</h5>
			<div class="input-container phone-input">
				<span class="input-icon bg-phone_icon"></span>
				<input type="text" name="phone" id="phone" placeholder="手机号">
				<span class="error-icon bg-tanhao_icon hidden"></span>
			</div>
			<div class="input-container half-container">
				<span class="input-icon bg-tick_icon"></span>
				<input  type="text" name="ver" class="ver-input" id="code" placeholder="验证码">
				<span class="error-icon bg-tanhao_icon hidden"></span>
				<span class="getver" id="get-code">获取验证码</span>
			</div>
            <div class="input-container password-input">
                <span class="input-icon bg-lock_icon"></span>
                <input type="password" name="password1" id="new_password"  placeholder="请输新密码">
                <span class="error-icon bg-tanhao_icon hidden"></span>
            </div>
			{{--<span class="worning-text">手机号码输入有误</span>--}}
            <div class="submit-btn" id="btn" style="text-align: center;line-height: 50px;">确定修改</div>
		</form>
    </div>
    <script>
        $(function(){
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;

            //获取短信验证码
            var countdown=60;
            var canGetCode = true;
            $('#get-code').click(function () {
                var obj = $(this);
                if(canGetCode){
                    var phone = $('#phone').val();
                    if(!myreg.test(phone)){
                        $.toast("请正确输入手机号码!", "forbidden");
                        return false;
                    }

                    $.ajax({
                        url:"{{route('Api.Auth.getVerifyCodeByPhone')}}",
                        type:"post",
                        dataType:"json",
                        data:{phone:phone},
                        success:function (object) {
                            if(object.level == 'success'){
                                $.toast(object.message,function(){
                                    settime(obj);
                                });
                            }else {
                                $.toast(object.message, "forbidden");
                            }
                        },
                        error:function (object) {
                            console.log(object);
                        }
                    });
                }
            });
            function settime(obj) { //发送验证码倒计时
                if (countdown == 0) {
                    canGetCode=true;
                    obj.removeClass('getting');
                    obj.html("获取验证码");
                    countdown = 60;
                    return;
                } else {
                    canGetCode=false;
                    obj.html("重新发送(" + countdown + ")");
                    obj.addClass('getting');
                    countdown--;
                }
                setTimeout(function(){settime(obj) },1000)
            }

            $('#btn').click(function(){
                var phone       = $('#phone').val();
                var newPassword = $('#new_password').val();
                var code        = $('#code').val();

                if(!myreg.test(phone)){
                    $.toast("请正确输入手机号码!", "forbidden");
                    return false;
                }

                if(newPassword.length < 6 || newPassword.length > 20){
                    $.toast("密码需6～20个字符!", "forbidden");
                    return false;
                }

                if(code.length != 4){
                    $.toast("请正确输入4位验证码", "forbidden");
                    return false;
                }

                $.ajax({
                    url:"{{route('Api.Auth.ResetPassword')}}",
                    type:"post",
                    dataType:"json",
                    data:{phone:phone,code:code,password:newPassword},
                    success:function (object) {
                        if(object.level == 'success'){
                            $.toast(object.message,function(){
                                window.location.href = "{{route('Home.Auth.login')}}";
                            });
                        }else {
                            $.toast(object.message, "forbidden");
                        }
                    },
                    error:function (object) {
                        console.log(object);
                    }
                });
            });
        });
    </script>
@endsection